package com.wxzz.elearing.livetelecast.api.app;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.livetelecast.dto.*;
import com.wxzz.elearing.livetelecast.entity.Activity;
import com.wxzz.elearing.livetelecast.entity.PlatformLivetelecast;
import com.wxzz.elearing.livetelecast.entity.WxUserPlatformLivetelecast;
import com.wxzz.elearing.livetelecast.service.IPlatformLivetelecastService;
import com.wxzz.elearing.livetelecast.service.IWxUserPlatformLivetelecastService;
import com.wxzz.elearning.common.utils.MD5Util;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 平台直播 前端控制器
 * </p>
 *
 * @author
 * @since 2021-04-02
 */
@RestController
@RequestMapping("/wx-platform-livetelecast")
@Api(tags = "平台直播(小程序)")
public class WxPlatformLivetelecastController {

    @Autowired
    private IPlatformLivetelecastService platformLivetelecastService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "平台直播列表(小程序)", notes = "平台直播列表(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "wxListPlatformLivetelecast", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<PlatformLivetelecast>>> wxListPlatformLivetelecast(@RequestBody WxPlatformLivetelecastDto wxPlatformLivetelecastDto) {
        return platformLivetelecastService.wxListPlatformLivetelecast(wxPlatformLivetelecastDto);
    }

    @ApiOperation(value = "更新在线人数(0-减一 1-加一)(小程序)", notes = "更新在线人数(0-减一 1-加一)(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "updateOnlinePersonNum/{platformLivetelecastId}/{update}", method = RequestMethod.POST)
    public ReturnVO updateOnlinePersonNum(@PathVariable Long platformLivetelecastId, @PathVariable Integer update) {
        return platformLivetelecastService.updateOnlinePersonNum(platformLivetelecastId, update);
    }

    @ApiOperation(value = "用户观看直播记录观看时间(小程序)", notes = "用户观看直播记录观看时间(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "seeReturn", method = RequestMethod.POST)
    public ReturnVO seeReturn(@RequestHeader String token, @RequestBody SeeReturnDto seeReturnDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return platformLivetelecastService.seeReturn(jsonObject.getLong("id"), seeReturnDto);
    }

    @ApiOperation(value = "平台直播详情(小程序)", notes = "平台直播详情(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectPlatformLivetelecastDetail/{platformLivetelecastId}", method = RequestMethod.POST)
    public ReturnVO<PlatformLivetelecast> selectPlatformLivetelecastDetail(@RequestHeader String token, @ApiParam("平台直播id") @PathVariable Long platformLivetelecastId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return platformLivetelecastService.selectWxPlatformLivetelecastDetail(jsonObject.getLong("id"), platformLivetelecastId);
    }

    @ApiOperation(value = "直播预约(小程序)", notes = "直播预约(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveWxUserPlatformLivetelecast", method = RequestMethod.POST)
    public ReturnVO<PlatformLivetelecast> saveWxUserPlatformLivetelecast(@RequestHeader String token, @RequestBody WxUserPlatformLivetelecastInsDto wxUserPlatformLivetelecastInsDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return platformLivetelecastService.saveWxUserPlatformLivetelecast(token, jsonObject, wxUserPlatformLivetelecastInsDto);
    }

    @ApiOperation(value = "展示我预约的直播(小程序)", notes = "展示我预约的直播(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listMyPlatformLivetelecast", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<PlatformLivetelecast>>> listMyPlatformLivetelecast(@RequestHeader String token, @RequestBody MyWxPlatformLivetelecastDto myWxPlatformLivetelecastDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return platformLivetelecastService.listMyPlatformLivetelecast(jsonObject.getLong("id"), myWxPlatformLivetelecastDto);
    }

    @Autowired
    private IWxUserPlatformLivetelecastService wxUserPlatformLivetelecastService;

    @ApiOperation(value = "展示我的看过的直播列表(小程序)", notes = "展示我的看过的直播列表(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectMyWatchPlatformLivetelecast", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<PlatformLivetelecast>>> selectMyWatchPlatformLivetelecast(@RequestHeader String token, @RequestBody MyWatchPlatformLivetelecastDto myWatchPlatformLivetelecastDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserPlatformLivetelecastService.selectMyWatchPlatformLivetelecast(jsonObject.getLong("id"), myWatchPlatformLivetelecastDto);
    }

    @ApiOperation(value = "直播打赏(小程序)", notes = "直播打赏(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "liveReward", method = RequestMethod.POST)
    public ReturnVO liveReward(@RequestHeader String token, @RequestBody LiveRewardDto liveRewardDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return platformLivetelecastService.liveReward(jsonObject, liveRewardDto);
    }

    @ApiOperation(value = "IM初始化", notes = "IM初始化", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "initIM/{platformLivetelecastId}", method = RequestMethod.POST)
    public ReturnVO<Map> initIM(@PathVariable("platformLivetelecastId") Long platformLivetelecastId) {
        return platformLivetelecastService.initIM(platformLivetelecastId);
    }

    @ApiOperation(value = "直播邀请排行榜", notes = "直播邀请排行榜", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listInvitedWxUser", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUserPlatformLivetelecast>>> listInvitedWxUser(@RequestBody ListInvitedWxUserDto listInvitedWxUserDto) {
        return platformLivetelecastService.listInvitedWxUser(listInvitedWxUserDto);
    }

    @ApiOperation(value = "查询自己的邀请", notes = "查询自己的邀请", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectMyInvited/{platformLivetelecastId}", method = RequestMethod.POST)
    public ReturnVO<WxUserPlatformLivetelecast> selectMyInvited(@RequestHeader String token, @PathVariable Long platformLivetelecastId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return platformLivetelecastService.selectMyInvited(jsonObject.getLong("id"), platformLivetelecastId);
    }

    @ApiOperation(value = "收藏或取消直播/活动", notes = "收藏或取消直播/活动", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "savePlatformLivetelecastCollect", method = RequestMethod.POST)
    public ReturnVO<WxUserPlatformLivetelecast> savePlatformLivetelecastCollect(@RequestHeader String token, @Valid @RequestBody PlatformLivetelecastCollectInsDto platformLivetelecastCollectInsDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserPlatformLivetelecastService.savePlatformLivetelecastCollect(jsonObject.getLong("id"), platformLivetelecastCollectInsDto);
    }

    @ApiOperation(value = "列出收藏的直播", notes = "列出收藏的直播", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listPlatformLivetelecastCollect", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<PlatformLivetelecast>>> listPlatformLivetelecastCollect(@RequestHeader String token, @RequestBody PlatformLivetelecastCollectDto platformLivetelecastCollectDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserPlatformLivetelecastService.listPlatformLivetelecastCollect(jsonObject.getLong("id"), platformLivetelecastCollectDto);
    }


    @ApiOperation(value = "列出收藏的活动", notes = "列出收藏的活动", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listActivityCollect", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Activity>>> listActivityCollect(@RequestHeader String token, @RequestBody ActivityCollectDto activityCollectDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserPlatformLivetelecastService.listActivityCollect(jsonObject.getLong("id"), activityCollectDto);
    }


//    @ApiOperation(value = "我预约的直播(小程序)", notes = "我预约的直播(小程序)", httpMethod = "POST", tags = "小程序")
//    @RequestMapping(value = "listMyPlatformLivetelecast", method = RequestMethod.POST)
//    public ReturnVO<PlatformLivetelecast> listMyPlatformLivetelecast(@RequestHeader String token, @RequestBody MyWxPlatformLivetelecastDto myWxPlatformLivetelecastDto) {
//        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
//        return platformLivetelecastService.listMyPlatformLivetelecast(jsonObject.getLong("id"), myWxPlatformLivetelecastDto);
//    }

}
